
#ifndef TIM_H_
#define TIM_H_


#include "include.h"

#define TIM_ClockFreq          (u8)(72u)
#define INPWM_RCCClockCmd()    do{RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);}while(0)
#define INPWM_TIM              (TIM2)
#define INPWM_TIM_IRQHandler   (TIM2_IRQHandler)
#define INPWM_TIM_ClockPreq    (u8)(6u)
#define INPWM_TIM_PSC          (u8)(TIM_ClockFreq/INPWM_TIM_ClockPreq-1)
#define INPWM_TIM_ARR          (u16)(65535u)

#define INPWM_TIM_MinValidFreq  (u32)(100ul)
#define INPWM_TIM_MaxValidFreq  (u32)(1000ul)


#define INPWM_TIM_OffSetFreqCnt  (u8)(20u)
#define INPWM_TIM_MinValidFreqCnt  (u32)(((1000000ul/INPWM_TIM_MinValidFreq)/10ul*1000000ul)/(1000000ul/INPWM_TIM_ClockPreq)*10+INPWM_TIM_OffSetFreqCnt)
#define INPWM_TIM_MaxValidFreqCnt  (u32)(((1000000ul/INPWM_TIM_MaxValidFreq))*1000000ul/((1000000ul/INPWM_TIM_ClockPreq)))

#define INPWM_FreqDiffValue        (u32)(2*100)
#define INPWM_DutyCycleDiffValue   (u32)(1*100)
#define INPWM_ConsistencyCheckCnt  (u32)(5u)

#define TIMBASE_RCCClockCmd()   do{RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);}while(0)
#define TIMBASE                (TIM3)
#define TIMBASE_IRQHandler     (TIM3_IRQHandler)
#define TIMBASE_ClockPreq      (u8)(1u)
#define TIMBASE_PSC            (u8)(TIM_ClockFreq/TIMBASE_ClockPreq-1)
#define TIMBASE_ARR            (u16)(1000-1u)

#define OUTPWM_RCCClockCmd()    do{RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);}while(0)
#define OUTPWM_TIM              (TIM1)
#define OUTPWM_TIM_ClockPreq    (u8)(72u)
#define OUTPWM_TIM_PSC          (u8)(TIM_ClockFreq/OUTPWM_TIM_ClockPreq-1)
#define OUTPWM_TIM_ARR          (u16)(3600-1u)  // 20kpwm  50us  

#define OFFPWMDuty               (u16)(0.35*10000)                
#define ONPWMDuty                (u16)(0.50*10000)        
#define MINPWMDuty               (u16)(0.55*10000)     
#define MAXPWMDuty               (u16)(0.85*10000)           


#define PWMDuty_0                (u16)(0)
#define PWMDuty_100              (u16)(1*10000)

#define MOTOR_SPEED_MIN_RPM     (u16)((OUTPWM_TIM_ARR+1)*0.3)
#define MOTOR_SPEED_MAX_RPM     (u16)((OUTPWM_TIM_ARR+1)*1)

//#define SPEED_K                 (u16)((float)(MOTOR_SPEED_MAX_RPM-MOTOR_SPEED_MIN_RPM)*100/(float)(MAXPWMDuty-MINPWMDuty))
#define SPEED_K                 (u16)(72u)

#define SlowStartTime           (u8)(30u)
#define OUTPWM_TIM_SingleVal    (u16)((OUTPWM_TIM_ARR+1)/100)


#define ON                      (1u)
#define OFF                     (0u)
//MOTOR_SPEED_MIN_RPM + SPEED_K * (dutyTemp - MINPWMDuty);   

typedef enum
{
	DutyCycle_InValid=0,
	DutyCycle_Valid,
}INPWM_DutyCycleENUM;

typedef struct
{
	u8 u8CheckCnt;
	u32 u32INPWM_CurFreq;
	u32 u32INPWM_LastFreq;
	u32 u32INPWM_AddFreq;
	u32 u32INPWM_CurDutyCycle;
	u32 u32INPWM_LastDutyCycle;
	u32 u32INPWM_AddDutyCycle;
	u32 u32INPWM_FinalFreq;
	u32 u32INPWM_FinalDutyCycle;
}INPWM_CheckType;

typedef enum
{
	No_Start=0,
	Speed_Init,
	Speed_Reach,
	Speed_NotReach,
}PWM_SpeedFlagENUM;

typedef enum
{
	UnChange=0,
	Rising,
	Falling,
}PWM_SpeedChangeENUM;

typedef struct
{
	PWM_SpeedFlagENUM PWM_SpeedFlag;
	u8 u8SpeedTimeCnt;
	PWM_SpeedChangeENUM PWM_SpeedChange;
	u32 u32TargetVal;
	u32 u32CurVal;
}PWM_SpeedControlFlagType;

typedef enum
{
	No_StartCapture=0,
	Capture_Ok,
	Capture_No,
}INPWM_CaptureFlagENUM;


extern void Tim_Config(void);
extern void PWM_MotorControl(void);
extern volatile u32 u32TimBaseCnt;
extern volatile u8 Uart_TXFlag;

#endif






